#!/usr/bin/env node

const http = require('http'),
  url = require('url');

let addr = process.argv[2];

function opt(addr){
  let options = url.parse(addr);
  options.headers = {'User-Agent':'06-redirection.js'};

  return options;
}

function get(options){
  http.get(options,(res)=>{
    console.log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.ststusMessage}`);
    console.log(res.headers);

    console.log('');

    if(res.statusCode < 400 && res.statusCode >= 300){
      get(opt(res.headers.location));
    }else{
      res.pipe(process.stdout);
    }
  });
}
get(opt(addr));
